<html>
<!-- =====================================================================

  File:      Generated file for Adventure Works Cycles Storefront Sample
  Summary:   Self-documentation for application
  Date:	     June 16, 2003

=====================================================================

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) 2003 Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= -->
<head>
  <link rel=stylesheet href=style.css>
</head>
<body>
<div class=SourcePanel style="font-size:12">
<pre style="background-color:white">
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Configuration</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Data</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Data</span>.<span style="color: #2040a0">SqlClient</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Globalization</span><span style="color: #4444FF">;</span>

<span style="color: #008000">/*=====================================================================

  File:      ShoppingCartDb.cs for Adventure Works Cycles Storefront Sample
  Summary:   Middle tier component for manipulating lists of pending items to be purchased.
  Date:	     June 16, 2003

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */</span>
<span style="color: #0000FF"><strong>namespace</strong></span> <span style="color: #2040a0">Microsoft</span>.<span style="color: #2040a0">Samples</span>.<span style="color: #2040a0">SqlServer</span>
<span style="color: #4444FF"><strong>{</strong></span>
	<span style="color: #008000">//*******************************************************</span>
	<span style="color: #008000">//</span>
	<span style="color: #008000">// ShoppingCartDB Class</span>
	<span style="color: #008000">//</span>
	<span style="color: #008000">// Business/Data Logic Class that encapsulates all data</span>
	<span style="color: #008000">// logic necessary to add/remove/update/purchase items</span>
	<span style="color: #008000">// within an AdventureWorks shopping cart.</span>
	<span style="color: #008000">//</span>
	<span style="color: #008000">//*******************************************************</span>
	<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>class</strong></span> <span style="color: #2040a0">ShoppingCartDB</span>
	<span style="color: #4444FF"><strong>{</strong></span>
		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// ShoppingCartDB.GetItems() Method &lt;a name=&quot;GetItems&quot;&gt;&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// The GetItems method returns a </span>
		<span style="color: #008000">// a forward-only, read-only DataReader.  This returns a list of all</span>
		<span style="color: #008000">// items within a shopping cart. </span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// Other relevant sources:</span>
		<span style="color: #008000">//     + &lt;a href=&quot;usp_ShoppingList.htm&quot; style=&quot;color:green&quot;&gt;usp_ShoppingList Stored Procedure&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #2040a0">SqlDataReader</span> <span style="color: #2040a0">GetItems</span><span style="color: #4444FF">(</span><span style="color: #2040a0">string</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #008000">// Create Instance of Connection and Command Object</span>
			<span style="color: #2040a0">SqlConnection</span> <span style="color: #2040a0">myConnection</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlConnection</span><span style="color: #4444FF">(</span>
                <span style="color: #2040a0">Properties</span>.<span style="color: #2040a0">Settings</span>.<span style="color: #2040a0">Default</span>.<span style="color: #2040a0">ConnectionString</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">SqlCommand</span> <span style="color: #2040a0">myCommand</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">CreateCommand</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">CommandText</span> <span style="color: #4444FF">=</span> <span style="color: #444444">&quot;usp_ShoppingList&quot;</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Mark the Command as a SPROC</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">CommandType</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">CommandType</span>.<span style="color: #2040a0">StoredProcedure</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Add Parameters to SPROC</span>
			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterCartID</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@CartID&quot;</span>,
															<span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">NVarChar</span>,
															<span style="color: #FF0000">50</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterCartID</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterCartID</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterCulture</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@Culture&quot;</span>,
															 <span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">NVarChar</span>,
															 <span style="color: #FF0000">10</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterCulture</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">CultureInfo</span>.<span style="color: #2040a0">CurrentUICulture</span>.<span style="color: #2040a0">Name</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterCulture</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Return the datareader result</span>
			<span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Open</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">ExecuteReader</span><span style="color: #4444FF">(</span><span style="color: #2040a0">CommandBehavior</span>.<span style="color: #2040a0">CloseConnection</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// ShoppingCartDB.AddItem() Method &lt;a name=&quot;AddItem&quot;&gt;&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// The AddItem method adds an item into a shopping cart.</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// Other relevant sources:</span>
		<span style="color: #008000">//     + &lt;a href=&quot;usp_ShoppingCartAddItem.htm&quot; style=&quot;color:green&quot;&gt;usp_ShoppingCartAddItem Stored Procedure&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>void</strong></span> <span style="color: #2040a0">AddItem</span><span style="color: #4444FF">(</span><span style="color: #2040a0">string</span> <span style="color: #2040a0">cartID</span>, <span style="color: #0000FF"><strong>int</strong></span> <span style="color: #2040a0">productID</span>, <span style="color: #0000FF"><strong>int</strong></span> <span style="color: #2040a0">quantity</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #008000">// Create Instance of Connection and Command Object</span>
			<span style="color: #2040a0">SqlConnection</span> <span style="color: #2040a0">myConnection</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlConnection</span><span style="color: #4444FF">(</span>
                <span style="color: #2040a0">Properties</span>.<span style="color: #2040a0">Settings</span>.<span style="color: #2040a0">Default</span>.<span style="color: #2040a0">ConnectionString</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">SqlCommand</span> <span style="color: #2040a0">myCommand</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlCommand</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;usp_ShoppingCartAddItem&quot;</span>,
												  <span style="color: #2040a0">myConnection</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Mark the Command as a SPROC</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">CommandType</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">CommandType</span>.<span style="color: #2040a0">StoredProcedure</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Add Parameters to SPROC</span>
			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterProductID</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@ProductID&quot;</span>,
															   <span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">Int</span>, <span style="color: #FF0000">4</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterProductID</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">productID</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterProductID</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterCartID</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@CartID&quot;</span>,
															<span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">NVarChar</span>,
															<span style="color: #FF0000">50</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterCartID</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterCartID</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterQuantity</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@Quantity&quot;</span>,
															  <span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">Int</span>, <span style="color: #FF0000">4</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterQuantity</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">quantity</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterQuantity</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Open the connection and execute the Command</span>
            <span style="color: #2040a0">try</span>
            <span style="color: #4444FF"><strong>{</strong></span>
                <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Open</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
                <span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">ExecuteNonQuery</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #4444FF"><strong>}</strong></span>
            <span style="color: #2040a0">finally</span>
            <span style="color: #4444FF"><strong>{</strong></span>
                <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Close</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #4444FF"><strong>}</strong></span>
        <span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// ShoppingCartDB.UpdateItem() Method &lt;a name=&quot;UpdateItem&quot;&gt;&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// The UpdateItem method updates the quantity of an item</span>
		<span style="color: #008000">// in a shopping cart.</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// Other relevant sources:</span>
		<span style="color: #008000">//     + &lt;a href=&quot;usp_ShoppingCartUpdate.htm&quot; style=&quot;color:green&quot;&gt;usp_ShoppingCartUpdate Stored Procedure&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>void</strong></span> <span style="color: #2040a0">UpdateItem</span><span style="color: #4444FF">(</span><span style="color: #2040a0">string</span> <span style="color: #2040a0">cartID</span>, <span style="color: #0000FF"><strong>int</strong></span> <span style="color: #2040a0">productID</span>, <span style="color: #0000FF"><strong>int</strong></span> <span style="color: #2040a0">quantity</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #008000">// throw an exception if quantity is a negative number</span>
			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">quantity</span> <span style="color: #4444FF">&lt;</span> <span style="color: #FF0000">0</span><span style="color: #4444FF">)</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #0000FF"><strong>throw</strong></span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">Exception</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;Quantity cannot be a negative number&quot;</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>

			<span style="color: #008000">// Create Instance of Connection and Command Object</span>
			<span style="color: #2040a0">SqlConnection</span> <span style="color: #2040a0">myConnection</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlConnection</span><span style="color: #4444FF">(</span>
                <span style="color: #2040a0">Properties</span>.<span style="color: #2040a0">Settings</span>.<span style="color: #2040a0">Default</span>.<span style="color: #2040a0">ConnectionString</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">SqlCommand</span> <span style="color: #2040a0">myCommand</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlCommand</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;usp_ShoppingCartUpdate&quot;</span>,
												  <span style="color: #2040a0">myConnection</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Mark the Command as a SPROC</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">CommandType</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">CommandType</span>.<span style="color: #2040a0">StoredProcedure</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Add Parameters to SPROC</span>
			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterProductID</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@ProductID&quot;</span>,
															   <span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">Int</span>, <span style="color: #FF0000">4</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterProductID</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">productID</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterProductID</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterCartID</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@CartID&quot;</span>,
															<span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">NVarChar</span>,
															<span style="color: #FF0000">50</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterCartID</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterCartID</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterQuantity</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@Quantity&quot;</span>,
															  <span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">Int</span>, <span style="color: #FF0000">4</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterQuantity</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">quantity</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterQuantity</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Open the connection and execute the Command</span>
            <span style="color: #2040a0">try</span>
            <span style="color: #4444FF"><strong>{</strong></span>
                <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Open</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
                <span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">ExecuteNonQuery</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #4444FF"><strong>}</strong></span>
            <span style="color: #2040a0">finally</span>
            <span style="color: #4444FF"><strong>{</strong></span>
                <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Close</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #4444FF"><strong>}</strong></span>
        <span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// ShoppingCartDB.RemoveItem() Method &lt;a name=&quot;RemoveItem&quot;&gt;&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// The RemoveItem method removes an item from a</span>
		<span style="color: #008000">// shopping cart.</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// Other relevant sources:</span>
		<span style="color: #008000">//     + &lt;a href=&quot;usp_ShoppingCartRemoveItem.htm&quot; style=&quot;color:green&quot;&gt;usp_ShoppingCartRemoveItem Stored Procedure&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>void</strong></span> <span style="color: #2040a0">RemoveItem</span><span style="color: #4444FF">(</span><span style="color: #2040a0">string</span> <span style="color: #2040a0">cartID</span>, <span style="color: #0000FF"><strong>int</strong></span> <span style="color: #2040a0">productID</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #008000">// Create Instance of Connection and Command Object</span>
			<span style="color: #2040a0">SqlConnection</span> <span style="color: #2040a0">myConnection</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlConnection</span><span style="color: #4444FF">(</span>
                <span style="color: #2040a0">Properties</span>.<span style="color: #2040a0">Settings</span>.<span style="color: #2040a0">Default</span>.<span style="color: #2040a0">ConnectionString</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">SqlCommand</span> <span style="color: #2040a0">myCommand</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlCommand</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;usp_ShoppingCartRemoveItem&quot;</span>,
												  <span style="color: #2040a0">myConnection</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Mark the Command as a SPROC</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">CommandType</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">CommandType</span>.<span style="color: #2040a0">StoredProcedure</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Add Parameters to SPROC</span>
			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterProductID</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@ProductID&quot;</span>,
															   <span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">Int</span>, <span style="color: #FF0000">4</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterProductID</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">productID</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterProductID</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterCartID</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@CartID&quot;</span>,
															<span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">NVarChar</span>,
															<span style="color: #FF0000">50</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterCartID</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterCartID</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Open the connection and execute the Command</span>
            <span style="color: #2040a0">try</span>
            <span style="color: #4444FF"><strong>{</strong></span>
                <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Open</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
                <span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">ExecuteNonQuery</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #4444FF"><strong>}</strong></span>
            <span style="color: #2040a0">finally</span>
            <span style="color: #4444FF"><strong>{</strong></span>
                <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Close</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #4444FF"><strong>}</strong></span>
        <span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// ShoppingCartDB.GetItemCount() Method &lt;a name=&quot;GetItemCount&quot;&gt;&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// The GetItemCount method returns the number of items</span>
		<span style="color: #008000">// within a shopping cart.</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// Other relevant sources:</span>
		<span style="color: #008000">//     + &lt;a href=&quot;usp_ShoppingCartItemCount.htm&quot; style=&quot;color:green&quot;&gt;usp_ShoppingCartItemCount Stored Procedure&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>int</strong></span> <span style="color: #2040a0">GetItemCount</span><span style="color: #4444FF">(</span><span style="color: #2040a0">string</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #008000">// Create Instance of Connection and Command Object</span>
			<span style="color: #2040a0">SqlConnection</span> <span style="color: #2040a0">myConnection</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlConnection</span><span style="color: #4444FF">(</span>
                <span style="color: #2040a0">Properties</span>.<span style="color: #2040a0">Settings</span>.<span style="color: #2040a0">Default</span>.<span style="color: #2040a0">ConnectionString</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">SqlCommand</span> <span style="color: #2040a0">myCommand</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlCommand</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;usp_ShoppingCartItemCount&quot;</span>,
												  <span style="color: #2040a0">myConnection</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Mark the Command as a SPROC</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">CommandType</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">CommandType</span>.<span style="color: #2040a0">StoredProcedure</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterCartID</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@CartID&quot;</span>,
															<span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">NVarChar</span>,
															<span style="color: #FF0000">50</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterCartID</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterCartID</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Add Parameters to SPROC</span>
			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterItemCount</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@ItemCount&quot;</span>,
															   <span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">Int</span>, <span style="color: #FF0000">4</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterItemCount</span>.<span style="color: #2040a0">Direction</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">ParameterDirection</span>.<span style="color: #2040a0">Output</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterItemCount</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Open the connection and execute the Command</span>
            <span style="color: #2040a0">try</span>
            <span style="color: #4444FF"><strong>{</strong></span>
                <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Open</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
                <span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">ExecuteNonQuery</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #4444FF"><strong>}</strong></span>
            <span style="color: #2040a0">finally</span>
            <span style="color: #4444FF"><strong>{</strong></span>
                <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Close</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #4444FF"><strong>}</strong></span>

            <span style="color: #008000">// Return the ItemCount (obtained as out paramter of SPROC)</span>
			<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #4444FF">(</span><span style="color: #4444FF">(</span><span style="color: #0000FF"><strong>int</strong></span><span style="color: #4444FF">)</span><span style="color: #2040a0">parameterItemCount</span>.<span style="color: #2040a0">Value</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// ShoppingCartDB.GetTotal() Method &lt;a name=&quot;GetTotal&quot;&gt;&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// The GetTotal method returns the total price of all</span>
		<span style="color: #008000">// items within the shopping cart.</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// Other relevant sources:</span>
		<span style="color: #008000">//     + &lt;a href=&quot;usp_ShoppingCartTotal.htm&quot; style=&quot;color:green&quot;&gt;usp_ShoppingCartTotal Stored Procedure&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #008000">// TODO: Switch back to Currency when this works.</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #2040a0">String</span> <span style="color: #2040a0">GetTotal</span><span style="color: #4444FF">(</span><span style="color: #2040a0">string</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #008000">// Create Instance of Connection and Command Object</span>
			<span style="color: #2040a0">SqlConnection</span> <span style="color: #2040a0">myConnection</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlConnection</span><span style="color: #4444FF">(</span>
                <span style="color: #2040a0">Properties</span>.<span style="color: #2040a0">Settings</span>.<span style="color: #2040a0">Default</span>.<span style="color: #2040a0">ConnectionString</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">SqlCommand</span> <span style="color: #2040a0">myCommand</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlCommand</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;usp_ShoppingCartTotal&quot;</span>,
												  <span style="color: #2040a0">myConnection</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Mark the Command as a SPROC</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">CommandType</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">CommandType</span>.<span style="color: #2040a0">StoredProcedure</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Add Parameters to SPROC</span>
			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterCartID</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@CartID&quot;</span>,
															<span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">NVarChar</span>,
															<span style="color: #FF0000">50</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterCartID</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterCartID</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterCulture</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@Culture&quot;</span>,
															 <span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">NVarChar</span>,
															 <span style="color: #FF0000">10</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterCulture</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">CultureInfo</span>.<span style="color: #2040a0">CurrentUICulture</span>.<span style="color: #2040a0">Name</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterCulture</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// TODO: Switch back to Currency when this works.</span>
			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">parameterTotalCost</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@TotalCost&quot;</span>,
															   <span style="color: #2040a0">SqlDbType</span>.
															   <span style="color: #2040a0">NVarChar</span>, <span style="color: #FF0000">20</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">parameterTotalCost</span>.<span style="color: #2040a0">Direction</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">ParameterDirection</span>.<span style="color: #2040a0">Output</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">//parameterTotalCost.UdtTypeName = &quot;[AdventureWorks.Currency]&quot;;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">parameterTotalCost</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Open the connection and execute the Command</span>
			<span style="color: #2040a0">try</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Open</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
				<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">ExecuteNonQuery</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>
			<span style="color: #2040a0">finally</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">State</span> <span style="color: #4444FF">!</span><span style="color: #4444FF">=</span> <span style="color: #2040a0">ConnectionState</span>.<span style="color: #2040a0">Closed</span><span style="color: #4444FF">)</span>
					<span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Close</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>

			<span style="color: #008000">// Return the Total</span>
			<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">String</span><span style="color: #4444FF">)</span><span style="color: #2040a0">parameterTotalCost</span>.<span style="color: #2040a0">Value</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// ShoppingCartDB.MigrateCart() Method &lt;a name=&quot;MigrateCart&quot;&gt;&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// The MigrateCart method migrates the items from one</span>
		<span style="color: #008000">// cartId to another.  This is used during the login</span>
		<span style="color: #008000">// and/or registration process to transfer a user's</span>
		<span style="color: #008000">// temporary cart items to a permanent account.</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// Other relevant sources:</span>
		<span style="color: #008000">//     + &lt;a href=&quot;usp_SetShoppingCartID.htm&quot; style=&quot;color:green&quot;&gt;usp_SetShoppingCartID Stored Procedure&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>void</strong></span> <span style="color: #2040a0">MigrateCart</span><span style="color: #4444FF">(</span><span style="color: #2040a0">String</span> <span style="color: #2040a0">oldCartId</span>, <span style="color: #2040a0">String</span> <span style="color: #2040a0">newCartId</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #008000">// Create Instance of Connection and Command Object</span>
			<span style="color: #2040a0">SqlConnection</span> <span style="color: #2040a0">myConnection</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlConnection</span><span style="color: #4444FF">(</span>
                <span style="color: #2040a0">Properties</span>.<span style="color: #2040a0">Settings</span>.<span style="color: #2040a0">Default</span>.<span style="color: #2040a0">ConnectionString</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">SqlCommand</span> <span style="color: #2040a0">myCommand</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlCommand</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;usp_SetShoppingCartID&quot;</span>,
												  <span style="color: #2040a0">myConnection</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Mark the Command as a SPROC</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">CommandType</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">CommandType</span>.<span style="color: #2040a0">StoredProcedure</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Add Parameters to SPROC</span>
			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">cart1</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@OriginalCartId &quot;</span>,
												  <span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">NVarChar</span>, <span style="color: #FF0000">50</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">cart1</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">oldCartId</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">cart1</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">cart2</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@NewCartId &quot;</span>,
												  <span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">NVarChar</span>, <span style="color: #FF0000">50</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">cart2</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">newCartId</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">cart2</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Open the connection and execute the Command</span>
            <span style="color: #2040a0">try</span>
            <span style="color: #4444FF"><strong>{</strong></span>
                <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Open</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
                <span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">ExecuteNonQuery</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #4444FF"><strong>}</strong></span>
            <span style="color: #2040a0">finally</span>
            <span style="color: #4444FF"><strong>{</strong></span>
                <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Close</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #4444FF"><strong>}</strong></span>
        <span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// ShoppingCartDB.EmptyCart() Method &lt;a name=&quot;EmptyCart&quot;&gt;&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// The EmptyCart method removes all current items within</span>
		<span style="color: #008000">// the shopping cart.</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// Other relevant sources:</span>
		<span style="color: #008000">//     + &lt;a href=&quot;usp_ShoppingCartEmpty.htm&quot; style=&quot;color:green&quot;&gt;usp_ShoppingCartEmpty Stored Procedure&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #0000FF"><strong>void</strong></span> <span style="color: #2040a0">EmptyCart</span><span style="color: #4444FF">(</span><span style="color: #2040a0">string</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #008000">// Create Instance of Connection and Command Object</span>
			<span style="color: #2040a0">SqlConnection</span> <span style="color: #2040a0">myConnection</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlConnection</span><span style="color: #4444FF">(</span>
                <span style="color: #2040a0">Properties</span>.<span style="color: #2040a0">Settings</span>.<span style="color: #2040a0">Default</span>.<span style="color: #2040a0">ConnectionString</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">SqlCommand</span> <span style="color: #2040a0">myCommand</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlCommand</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;usp_ShoppingCartEmpty&quot;</span>,
												  <span style="color: #2040a0">myConnection</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Mark the Command as a SPROC</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">CommandType</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">CommandType</span>.<span style="color: #2040a0">StoredProcedure</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Add Parameters to SPROC</span>
			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">cartid</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@CartID&quot;</span>,
												   <span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">NVarChar</span>, <span style="color: #FF0000">50</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">cartid</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">cartid</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// Open the connection and execute the Command</span>
            <span style="color: #2040a0">try</span>
            <span style="color: #4444FF"><strong>{</strong></span>
                <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Open</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
                <span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">ExecuteNonQuery</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #4444FF"><strong>}</strong></span>
            <span style="color: #2040a0">finally</span>
            <span style="color: #4444FF"><strong>{</strong></span>
                <span style="color: #2040a0">myConnection</span>.<span style="color: #2040a0">Close</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
            <span style="color: #4444FF"><strong>}</strong></span>
        <span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// ShoppingCartDB.GetShoppingCartId() Method &lt;a name=&quot;GetShoppingCartId&quot;&gt;&lt;/a&gt;</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// The GetShoppingCartId method is used to calculate the</span>
		<span style="color: #008000">// &quot;ShoppingCart&quot; ID key used for a tracking a browser.</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">// The ShoppingCartID value is either the User's Identity</span>
		<span style="color: #008000">// Name (if they are a registered and authenticated user),</span>
		<span style="color: #008000">// or a random GUID calculated for guest visitors or</span>
		<span style="color: #008000">// customers who have not yet logged in.</span>
		<span style="color: #008000">//</span>
		<span style="color: #008000">//*******************************************************</span>
		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #2040a0">String</span> <span style="color: #2040a0">GetShoppingCartId</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #008000">// Obtain current HttpContext of ASP+ Request</span>
			<span style="color: #2040a0">System</span>.<span style="color: #2040a0">Web</span>.<span style="color: #2040a0">HttpContext</span> <span style="color: #2040a0">context</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Web</span>.<span style="color: #2040a0">HttpContext</span>.<span style="color: #2040a0">Current</span><span style="color: #4444FF">;</span>

			<span style="color: #008000">// If the user is authenticated, use their customerId as a permanent shopping cart id</span>
			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">context</span>.<span style="color: #2040a0">User</span>.<span style="color: #2040a0">Identity</span>.<span style="color: #2040a0">Name</span>.<span style="color: #2040a0">Length</span> <span style="color: #4444FF">!</span><span style="color: #4444FF">=</span> <span style="color: #FF0000">0</span><span style="color: #4444FF">)</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">context</span>.<span style="color: #2040a0">User</span>.<span style="color: #2040a0">Identity</span>.<span style="color: #2040a0">Name</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>

			<span style="color: #008000">// If user is not authenticated, either fetch (or issue) a new temporary cartID</span>
			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">context</span>.<span style="color: #2040a0">Request</span>.<span style="color: #2040a0">Cookies</span><span style="color: #4444FF">[</span><span style="color: #444444">&quot;AdventureWorks_CartID&quot;</span><span style="color: #4444FF">]</span> <span style="color: #4444FF">!</span><span style="color: #4444FF">=</span> <span style="color: #2040a0">null</span><span style="color: #4444FF">)</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">context</span>.<span style="color: #2040a0">Request</span>.<span style="color: #2040a0">Cookies</span><span style="color: #4444FF">[</span><span style="color: #444444">&quot;AdventureWorks_CartID&quot;</span><span style="color: #4444FF">]</span>.<span style="color: #2040a0">Value</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>
			<span style="color: #0000FF"><strong>else</strong></span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #008000">// Generate a new random GUID using System.Guid Class</span>
				<span style="color: #2040a0">Guid</span> <span style="color: #2040a0">tempCartId</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">Guid</span>.<span style="color: #2040a0">NewGuid</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

				<span style="color: #008000">// Send tempCartId back to client as a cookie</span>
				<span style="color: #2040a0">context</span>.<span style="color: #2040a0">Response</span>.<span style="color: #2040a0">Cookies</span><span style="color: #4444FF">[</span><span style="color: #444444">&quot;AdventureWorks_CartID&quot;</span><span style="color: #4444FF">]</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span>
				<span style="color: #2040a0">tempCartId</span>.
																		  <span style="color: #2040a0">ToString</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

				<span style="color: #008000">// Return tempCartId</span>
				<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">tempCartId</span>.<span style="color: #2040a0">ToString</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #0000FF"><strong>public</strong></span> <span style="color: #2040a0">SqlDataReader</span> <span style="color: #2040a0">GetProductRecommendationsFromCart</span><span style="color: #4444FF">(</span><span style="color: #2040a0">String</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
            <span style="color: #2040a0">SqlConnection</span> <span style="color: #2040a0">conn</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlConnection</span><span style="color: #4444FF">(</span><span style="color: #2040a0">Properties</span>.<span style="color: #2040a0">Settings</span>.<span style="color: #2040a0">Default</span>.<span style="color: #2040a0">ConnectionString</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">SqlCommand</span> <span style="color: #2040a0">myCommand</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">conn</span>.<span style="color: #2040a0">CreateCommand</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">CommandText</span> <span style="color: #4444FF">=</span> <span style="color: #444444">&quot;usp_ShoppingCartGetProductRecommendations&quot;</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">CommandType</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">CommandType</span>.<span style="color: #2040a0">StoredProcedure</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">SqlParameter</span> <span style="color: #2040a0">cartParam</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">SqlParameter</span><span style="color: #4444FF">(</span><span style="color: #444444">&quot;@CartID&quot;</span>,
													  <span style="color: #2040a0">SqlDbType</span>.<span style="color: #2040a0">NVarChar</span>, <span style="color: #FF0000">50</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

			<span style="color: #2040a0">cartParam</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">cartID</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">Parameters</span>.<span style="color: #2040a0">Add</span><span style="color: #4444FF">(</span><span style="color: #2040a0">cartParam</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #2040a0">conn</span>.<span style="color: #2040a0">Open</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
			<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">myCommand</span>.<span style="color: #2040a0">ExecuteReader</span><span style="color: #4444FF">(</span><span style="color: #2040a0">CommandBehavior</span>.<span style="color: #2040a0">CloseConnection</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>
	<span style="color: #4444FF"><strong>}</strong></span>
<span style="color: #4444FF"><strong>}</strong></span>

</pre>
